package Constructions;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URLClassLoader;

import javax.imageio.ImageIO;

import Announcements.Announcement;
import Events.CrewArrival;
import Main.SpaceGame;
import Menus.HireMenu;

public class TeleportPad extends Construction
{
	int x,y;
	BufferedImage sprite = null;
	SpaceGame main;
	int beamHeight = 0, numPeople = 0;
	boolean teleporting = false;

	public TeleportPad(int x, int y,SpaceGame main) 
	{
		super(x,y,main);
		
		height = 2;width = 3;
		this.main = main;
		this.x = x;
		this.y = y;
		try {
			sprite = ImageIO.read(URLClassLoader.getSystemResource("Resources/"+"TeleportPad.png"));
		}
		catch (IOException e) {
			e.printStackTrace();
		}
		workingTile = main.world.tiles[x][y];
		
		setText();
	}
	
	public void draw(Graphics g) 
	{
		g.drawImage(sprite, x*10, y*10, null);
		
		if (teleporting)
		{
			g.setColor(Color.cyan);
			if (numPeople > 6)
				numPeople = 6;
			for(int i = 0;i<numPeople;i++)
			{
				int thisX = x+((int)(i/2+.5)-1);
				int thisHeight = (int)(beamHeight/3.5);
				if (i%2 == 0)
					g.fillRect(thisX*10+11, y*10+(Math.abs((int)((beamHeight-35)/3.5)))+10, 8, thisHeight);
				else
					g.fillRect(thisX*10+11, (y*10+(Math.abs((int)((beamHeight-35)/3.5)))), 8, thisHeight);
			}
		}
		if (beamHeight >= 35)
			teleporting = false;
		drawFlash(g,x,y);
	}
	
	public void update()
	{
		checkPowered(x,y);
	}
	
	public String getName() 
	{
		return "Teleport Pad";
	}
	
	public void setText() 
	{
		textColorIndex = 2;
		
		colors.add(Color.cyan);
		colors.add(Color.cyan);
		text.add("");
		text.add(getName());
		
		colors.add(Color.cyan);
		colors.add(Color.cyan);
		text.add("");
		text.add("");	
		
		if(powered)
		{
			colors.add(Color.green);
			text.add("h");
			colors.add(Color.cyan);
			text.add("- Hire Crew");
		}
		
		else
		{
			colors.add(Color.red);
			colors.add(Color.red);
			text.add("Needs Power");
			text.add("");
		}
	}

	public void keyPressed(KeyEvent e) 
	{
		if (e.getKeyCode() == KeyEvent.VK_H)
		{
				main.menuManager.addMenu(new HireMenu(main));
		}
	}

	public void keyReleased(KeyEvent arg0) 
	{
		
	}

	public void keyTyped(KeyEvent arg0) 
	{
		
	}
	
	public void drawTeleport(CrewArrival event)
	{
		this.beamHeight = Math.abs(event.getDuration()-35);
		teleporting = true;
		numPeople = event.entities.size();
	}
}
